package com.yidu.qx.action;

import com.yidu.qx.action.vo.UserEntityVo;
import com.yidu.qx.action.vo.UserVo;
import com.yidu.qx.domain.QxUserEntity;
import com.yidu.qx.service.CharacterService;
import com.yidu.qx.service.UserService;
import com.yidu.qx.utils.BaseMessage;
import com.yidu.qx.utils.Pages;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @program: jurisdiction
 * @description
 * @author: 周佳佳
 * @create: 2019-09-27 10:21
 **/
@Controller
@RequestMapping("/userController")
public class UserController {
    Logger logger= (Logger) Logger.getLogger(String.valueOf(UserController.class));
    @Resource
    UserService service;
    @Resource
    CharacterService characterService;
    @ResponseBody
    @RequestMapping("findAll")
    public Map<String,Object>  findAll(Pages pages, UserEntityVo userEntityVo, String page){
        logger.info("进来了==========================================");
        pages.setCurPage(Integer.valueOf(page));
        List<UserEntityVo> list=service.findAll(userEntityVo,pages);
        Map<String, Object> map = new HashMap<String, Object>();
        pages.setTotalRows(service.count(userEntityVo).intValue());
        map.put("code", 0);
        map.put("msg", "");
        map.put("count",pages.getTotalRows());
        map.put("data", list);
        return map;
    }
    @ResponseBody
    @RequestMapping("checked")
    public BaseMessage checked(QxUserEntity qxUserEntity1){
        logger.info(qxUserEntity1.getUserState()+"============================================");
        BaseMessage message=new BaseMessage();
        QxUserEntity qxUserEntity2=service.findById(qxUserEntity1.getUserId());
        qxUserEntity2.setUserState(qxUserEntity1.getUserState());
        message.setState(1);
        service.update(qxUserEntity2);
        return message;
    }
    @ResponseBody
    @RequestMapping("findJueseAll")
    public List<UserEntityVo> list(){
        List<UserEntityVo> list=characterService.findAllone();
        return list;
    }
    @ResponseBody
    @RequestMapping("insert")
    public BaseMessage insert(UserEntityVo userEntityVo,HttpServletRequest request){
        userEntityVo.setUserState(userEntityVo.getOneState());
        userEntityVo.setChaId(userEntityVo.getOnechaId());
        logger.info(userEntityVo.getUserName()+"333=======================================");
        logger.info("上传图片进来了================================================");
        logger.info(userEntityVo.getUserName()+"==============================");
        logger.info(userEntityVo.getUserPhone()+"==============================");
        logger.info(userEntityVo.getUserState()+"==============================");
        logger.info(userEntityVo.getChaId()+"==============================");
        String path = request.getSession().getServletContext().getRealPath("/upload");
        String fileName = userEntityVo.getUserdImg().getOriginalFilename();
        if(fileName!=""&&fileName!=null) {
            File targetFile = new File(path, fileName);
            if(!targetFile.exists()){
                targetFile.mkdirs();
            }
            //保存
            try {
                userEntityVo.getUserdImg().transferTo(targetFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
            userEntityVo.setUserImg("upload/"+fileName);
        }
        if(userEntityVo.getUserId()!=null &&!"".equals(userEntityVo.getUserId())){
            logger.info(userEntityVo.getUserName()+"=======================================");
            userEntityVo.setUserName(userEntityVo.getOneName());
            service.update(userEntityVo);
        }else{
            service.add(userEntityVo);
        }
        BaseMessage baseMessage=new BaseMessage();
        baseMessage.setState(1);
        return baseMessage;
    }
    @ResponseBody
    @RequestMapping("findName")
    public BaseMessage findName(String userName,String userId){
        List<QxUserEntity> qxUserEntity=service.findName(userName);
        BaseMessage baseMessage=new BaseMessage();
        for (QxUserEntity userEntity : qxUserEntity) {
            if(userEntity!=null){
                baseMessage.setMsg("用户名已存在,请换一个!!");
                baseMessage.setState(1);
            }else{
                baseMessage.setState(3);
            }
            if(userId!=null&&!"".equals(userId)){
                baseMessage.setState(2);
            }

        }
        return baseMessage;
    }
    @ResponseBody
    @RequestMapping("findByIdThree")
    public UserEntityVo findByIdThree(String userId){

        UserEntityVo userEntityVo=service.findByIdThree(userId);
        return userEntityVo;
    }

    @RequestMapping("userLogin")
    @ResponseBody
    public BaseMessage userLogin(String userName, String userPassword, HttpServletRequest request){
        System.err.println("=================================================账号"+userName+"密码:"+userPassword);

        UserVo vo = service.find(userName, userPassword);

        BaseMessage message = new BaseMessage();

        //如果结果集不是空的
        //System.err.println("控制器的输出语句"+vo.getUname()+vo.getUpassword()+vo.getUmail());
        if(vo!=null) {
            message.setState(1);

            HttpSession session = request.getSession();

            session.setAttribute("user", vo);

        }else{
            message.setState(0);
        }

        return message;

    }
}
